package com.htc.model.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.htc.constants.Constants;
import com.htc.model.dto.SpaParentDTO;

public class SpaParentModel {
	public static SpaParentModel instance;

	public static SpaParentModel getInstance() {
		if (instance == null)
			instance = new SpaParentModel();
		return instance;
	}

	public boolean checkDB(Context context, String id) {
		boolean flag = false;
		String sql = "SELECT spa_category_id FROM "
				+ Constants.TBL_SPA_CATEGORY + " where spa_category_id='" + id
				+ "'";
		DBHelper db = DBHelper.getInstance(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertSpapa(SpaParentDTO spap, Context context) {
		boolean flag = false;
		if (!checkDB(context, spap.get_id())) {
			String DATABASE_TABLE = Constants.TBL_SPA_CATEGORY;
			ContentValues values = new ContentValues();
			values.put("spa_category_id", spap.get_id());
			values.put("spa_category_title", spap.getSpaTitle());
			values.put("priority", spap.getPriority());
			values.put("spa_category_title_ar", spap.getSpaTitleAr());
			DBHelper db = DBHelper.getInstance(context);
			flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
			db.close();
		}

		return flag;
	}

	public boolean updateSpapa(SpaParentDTO spap, Context context) {
		boolean flag = false;

		String DATABASE_TABLE = Constants.TBL_SPA_CATEGORY;
		String where = "spa_category_id='" + spap.get_id() + "'";
		ContentValues values = new ContentValues();

		values.put("spa_category_title", spap.getSpaTitle());
		values.put("priority", spap.getPriority());
		values.put("spa_category_title_ar", spap.getSpaTitleAr());
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteUpdate(null, DATABASE_TABLE, values, where, null);
		db.close();
		return flag;
	}

	public void deleteSpapa(SpaParentDTO spap, Context context) {
		String sql = "spa_category_id='" + spap.get_id() + "'";
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = Constants.TBL_SPA_CATEGORY;
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}
}
